(function (a2dmah5) {
    a2dmah5.addExpandPage(0, function (get) {
        const Common = get('UtilsCommon', 'Common');

        return {
            expandInit() {
                this.saveEntityData2 = this.getProperty('saveEntityData2');
                this.saveEntityData2.isVisible = !this.pageData.SaleOrderId || this.pageData.isCopy === 'true';
                this.accessoryDetails = this.getProperty('AccessoryDetails');
                this.accessoryDetails.setDataLoad = this.accessoryDetailsChange.bind(this);
                this.materialDetails = this.getProperty('MaterialDetails');
                this.materialDetails.setDataLoad = this.materialDetailsChange.bind(this);

                this.detailsProperty = this.getProperty('Details');
                this.detailsProperty.setDataLoad = this.detailsChange.bind(this);

                this.isDataLoad = !this.pageData.SaleOrderId;

                this.productDetails = {};

                const entityEdit2 = this.getProperty('entityEdit2');
                entityEdit2.properties.forEach(p => {
                    if (p.name === 'ProductAmount') { this.productAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'FreightAmount') { this.freightAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'OtherAmount') { this.otherAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'DiscountAmount') { this.discountAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'InstallAmount') { this.installAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                    else if (p.name === 'TotalAmount') { this.totalAmountProperty = p; p.valueChange = this.setTotalAmount.bind(this); }
                });

                productEdit = this.getProperty('productEdit');

                productEdit.properties.forEach(p => {
                    if (p.name === 'ProductTypeId') this.productTypeIdProperty = p;
                    else if (p.name === 'ProductId') { this.productIdProperty = p; p.valueChange = this.productIdChange.bind(this); }
                    else if (p.name === 'Area') this.areaProperty = p;
                    else if (p.name === 'Spec') this.specProperty = p;
                    else if (p.name === 'Price') this.priceProperty = p;
                    else if (p.name === 'ProduceAmount') { this.produceAmountProperty = p; p.valueChange = this.setProductAmount.bind(this); }
                    else if (p.name === 'InstallAmount') { this.installAmountProperty2 = p; p.valueChange = this.setInstallAmount.bind(this); }
                    else if (p.name === 'DiscountAmount') { this.discountAmountProperty2 = p; p.valueChange = this.setProductAmount.bind(this); }
                    else if (p.name === 'Num') { this.numProperty = p; p.valueChange = this.numChange.bind(this); }
                    else if (p.name === 'Amount') this.amountProperty = p;
                    else if (p.name === 'TotalInstallAmount') this.totalInstallAmountProperty = p;
                });
            },
            setProductEditData({ entityData }) {
                entityData.ProductTypeName = this.productTypeIdProperty.getText();
                entityData.ProductName = this.productIdProperty.getText();
                return entityData;
            },
            setProductAmount(value, property, view, price) {
                if (property && !property.isChanged) return;

                if (price === undefined) price = Common.getFloat(this.priceProperty.getValue());
                const num = Common.getFloat(this.numProperty.getValue());
                const produceAmount = Common.getFloat(this.produceAmountProperty.getValue());
                const discountAmount = Common.enlarge(this.discountAmountProperty2.getValue());
                let amount = Common.enlarge(price * num, 2);
                const pamount = Common.enlarge(produceAmount * num, 2);

                amount = (amount + pamount - discountAmount) / 100;
                this.amountProperty.setValue(amount);
            },
            setInstallAmount(value, property) {
                if (property && !property.isChanged) return;
                const num = Common.getFloat(this.numProperty.getValue());
                const installAmount = Common.getFloat(this.installAmountProperty2.getValue());
                const totalInstallAmount = Common.getNumber(installAmount * num, 2);
                this.totalInstallAmountProperty.setValue(totalInstallAmount)
            },
            numChange(value, property) {
                if (!property.isChanged) return;

                this.setProductAmount();
                this.setInstallAmount();
            },
            productIdChange(value, selectData, property) {
                if (!property.isChanged) return;

                if (selectData) {
                    const { Area, Spec, ProduceAmount, InstallAmount } = selectData;
                    this.areaProperty.setValue(Area);
                    this.specProperty.setValue(Spec);
                    this.produceAmountProperty.setValue(ProduceAmount);
                    this.installAmountProperty2.setValue(InstallAmount);
                }

                if (value) {
                    const detail = this.productDetails[value];
                    if (detail) {
                        this.accessoryDetails.setValue(detail.Details);
                        this.materialDetails.setValue(detail.Details2);
                    }
                    else {
                        const { serviceName, actionName } = property.changeServiceInterface;
                        const pathQuery = '(' + value + ')';
                        this.dispatchAction(serviceName, actionName, { pathQuery }).then(res => {
                            if (this.isSuccessProps(res)) {
                                this.productDetails[value] = res;
                                res.Details.forEach(d => {
                                    if (d.Amount === undefined) d.Amount = Common.getNumber(d.Num * d.OutPrice);
                                })
                                this.accessoryDetails.setValue(res.Details);
                                res.Details2.forEach(d => {
                                    if (d.Amount === undefined) d.Amount = Common.getNumber(d.Num * d.OutPrice);
                                })
                                this.materialDetails.setValue(res.Details2);
                            }
                        });
                    }
                }
                else {
                    this.accessoryDetails.setValue([]);
                    this.materialDetails.setValue([]);
                    this.setProductAmount(null, null, null, 0);
                }

                this.setInstallAmount();
            },
            expandSetEntityData({ entityData }) {
                entityData.Status = 0;
                let blExists = false;
                const productIdList = [];

                for (let i = 0; i < entityData.Details.length; i++) {
                    const id = entityData.Details[i].ProductId;
                    if (productIdList.includes(id)) { blExists = true; break; }
                    else productIdList.push(id);
                }

                if (blExists) {
                    this.alert('产品有重复添加');
                    return false;
                }

                return entityData;
            },
            expandDataLoad({ data }) {
                if ((data.Status == 0 || data.Status == 1) && Common.isEquals(data.CreateUser, this.loginUser.UserId, true)) this.saveEntityData2.setIsVisible(true);
                setTimeout(() => this.isDataLoad = true, 100);
            },
            accessoryDetailsChange(dataList) {
                this.detailsChange2(dataList, this.materialDetails.getValue());
            },
            materialDetailsChange(dataList) {
                this.detailsChange2(this.accessoryDetails.getValue(), dataList);
            },
            detailsChange(dataList) {
                let productAmount = 0;
                dataList = dataList || [];
                if (dataList.length === 0 || !this.isDataLoad) return;

                let installAmount = 0;

                dataList.forEach(d => {
                    productAmount += Common.enlarge(d.Amount);
                    installAmount += Common.enlarge(d.TotalInstallAmount);
                });

                productAmount = productAmount / 100;
                installAmount = installAmount / 100;

                this.productAmountProperty.setValue(productAmount);
                this.installAmountProperty.setValue(installAmount);
                this.setTotalAmount(null, null, null, productAmount, installAmount);
            },
            detailsChange2(dataList, dataList2) {
                let productAmount = 0;
                dataList = dataList || [];
                dataList2 = dataList2 || [];

                if (dataList.length === 0 || !this.isDataLoad) return;

                dataList.forEach(d => {
                    productAmount += Common.enlarge(d.Amount);
                });

                dataList2.forEach(d => {
                    productAmount += Common.enlarge(d.Amount);
                });

                productAmount = productAmount / 100;

                this.priceProperty.setValue(productAmount);
                this.setProductAmount(null, null, null, productAmount);
            },
            setTotalAmount(value, property, view, productAmount, installAmount) {
                if (property && !property.isChanged) return;
                if (productAmount === undefined) productAmount = Common.enlarge(this.productAmountProperty.getValue());
                else productAmount = Common.enlarge(productAmount);

                if (installAmount === undefined) installAmount = Common.enlarge(this.installAmountProperty.getValue());
                else installAmount = Common.enlarge(installAmount);

                let freightAmount = Common.enlarge(this.freightAmountProperty.getValue());
                let otherAmount = Common.enlarge(this.otherAmountProperty.getValue());
                let discountAmount = Common.enlarge(this.discountAmountProperty.getValue());

                let totalAmount = productAmount + freightAmount + installAmount + otherAmount - discountAmount;
                totalAmount = totalAmount / 100;
                this.totalAmountProperty.setValue(totalAmount);
            },
            setRowDataProperties({ view }) {
                view.properties.forEach(p => {
                    if (p.name === 'MaterialProductId') p.valueChange = this.materialProductIdChange.bind(this);
                    else if (p.name === 'OutPrice' || p.name === 'Num' || p.name === 'DiscountAmount') p.valueChange = this.inputValueChange.bind(this);
                });
            },
            materialProductIdChange(value, selectData, property, view) {
                if (!property.isChanged) return;
                if (selectData) this.computeAmount(view, selectData.OutPrice, selectData.InPrice);
                property.isChanged = false;
            },
            inputValueChange(value, property, view) {
                if (!property.isChanged) return;
                this.computeAmount(view);
                property.isChanged = false;
            },
            computeAmount(view, price2, inPrice) {
                let price = price2 || 0;
                let num = 0;
                let discountAmount = 0;
                let amount = 0;
                let amountProperty;
                view.properties.forEach(p => {
                    if (p.name === 'OutPrice') {
                        if (price2 !== undefined) {
                            p.setValue(price2);
                            p.data.OutPrice = price2;
                            p.data.InPrice = inPrice;
                        }
                        else price = Common.getFloat(p.data.OutPrice);
                    }
                    else if (p.name === 'Num') num = Common.getFloat(p.data.Num, 3);
                    else if (p.name === 'DiscountAmount') discountAmount = Common.enlarge(p.data.DiscountAmount);
                    else if (p.name === 'Amount') amountProperty = p;
                });

                let a = Common.enlarge(price * num, 2);
                amount = (a - discountAmount) / 100;
                amountProperty.setValue(amount);
                amountProperty.data.Amount = amount;
                this.detailsChange2(this.accessoryDetails.getValue(), this.materialDetails.getValue());
            }
        }
    });

})(window.a2dmah5);